<?php 
$pagina = $_GET['pagina'];
$classe = $_GET['classe'];
$id 	= $_GET["id"];

require_once "includes/head.php";
require_once "includes/header.php";

require_once "classes/ImageManipulation.php"; //ARQUIVO DA CLASSE CROP
require_once "classes/$classe.php"; //ARQUIVO DA CLASSE

$crop = new $classe();
$crop->Carregar($id);

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $src = $crop->GetDiretorio().$crop->GetImagemNome();
    $objImage = new ImageManipulation($src);
	
    if( $objImage->imageok ) 
	{
        $objImage->setCrop($_POST['x'], $_POST['y'], $_POST['w'], $_POST['h']);
        //$objImage->resize(1000);
        //$objImage->show();        
        $objImage->save($crop->GetDiretorio().$crop->GetImagemNome());
		clearstatcache(); 
		
		$_SESSION["mensagens"]["alerta"] = "<div class='notice success'><span>Imagem recortada com <strong>sucesso!</strong></span></div>";		
		header("location:$pagina.php");		
    }
    else
        echo 'Error!';

    exit;
}


if($_GET)
{
?>
    <script src="js/crop/js/jquery.Jcrop.pack.js" type="text/javascript"></script>
    <link rel="stylesheet" href="js/crop/css/jquery.Jcrop.css" type="text/css" />
	<script type="text/javascript">	
		$(function(){
			$('#cropbox').Jcrop({
				//aspectRatio: 1,
				onSelect    : updateCoords,					
				onChange    : showCoords,			
				minSize		: [ <?php echo $crop->GetMinLargura().",".$crop->GetMinAltura();?> ], 
				maxSize		: [ <?php echo $crop->GetMaxLargura().",".$crop->GetMaxAltura();?> ],
				allowResize	: false,
				addClass	: 'custom'
			});
		});

		function updateCoords(c)
		{
			$('#x').val(c.x);
			$('#y').val(c.y);
			$('#w').val(c.w);
			$('#h').val(c.h);
		};
		
		function showCoords(c)
	    {
	      $('.w').val(c.w);
	      $('.h').val(c.h);
	    };
	</script>
	
	
	<div class="panel-wrapper" style='text-align: center;'>	
		<div class="panel-wrapper fixed" style="display: inline-block"><?php echo $_SESSION["mensagens"]["alerta"];?>
					        
			<div class="panel">				
				<div class="title">
					<h4>recortar imagem</h4>	
				</div>
				
				<label>Largura em px: <input type="text" size="4" class="w" name="w" /></label>
	      		<label>Altrua em px: <input type="text" size="4" class="h" name="h" /></label>
				
				<form action="" method="post" enctype="multipart/form-data" onsubmit="return checkCoords();">					
		            <input type="hidden" id="x" name="x" />
		            <input type="hidden" id="y" name="y" />
		            <input type="hidden" id="w" name="w" />
		            <input type="hidden" id="h" name="h" />			
					
                    <input type="submit" name="crop" value="Recortar Imagem" class="button-red" />
                    <a href='teste_imagem.php' alt='voltar' class="button-white">Não Recortar</a>                                  
				</form>
				
				<img src="<?php echo $crop->GetDiretorio().$crop->GetImagemNome();?>" id="cropbox" />		
			</div><!-- panel -->
			<div class="shadow"></div>	
			
		</div><!-- panel-wrapper fixed -->	
	</div><!-- panel-wrapper -->

<?php
}		
	 
?>